﻿using ITSM.Model.Model;
using ITSM.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ITSM.Controllers.Demand
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemandOperateController : ControllerBase
    {
        private readonly IDemandOperate _demandOperate;
        private readonly IDemand _demand;
        private readonly IDemandTypeLink _demandTypeLink;
        private readonly IKnowledgeBase _knowledgeBase ;
        public DemandOperateController(IDemandOperate demandOperate , IDemand demand, IDemandTypeLink demandTypeLink, IKnowledgeBase knowledgeBase)
        {
            _demandOperate = demandOperate;
            _demand = demand;
            _demandTypeLink = demandTypeLink;
            _knowledgeBase = knowledgeBase;
        }

        [HttpGet("/api/GetTransferOrdersUsers")]
        public IActionResult GetTransferOrdersUsers(string RYD_Company)
        {
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            var data = _demandOperate.GetTransferOrdersUsers(RYD_Company);
            return Ok(data);
        }
        [HttpGet("/api/GetCompanyTransferOrdersUsers")]
        public IActionResult GetCompanyTransferOrdersUsers(string RYD_Company)
        {
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            var data = _demandOperate.GetCompanyTransferOrdersUsers(RYD_Company);
            return Ok(data);
        }

        [HttpPut("/api/TransferOrders")]
        public IActionResult TransferOrders(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId) || string.IsNullOrWhiteSpace(operate.RYD_ToUserId)|| operate.RYD_ToType==0)
            {
                return NotFound();
            }
            if (_demandOperate.TransferOrders(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/EvaluationOrder")]
        public IActionResult EvaluationOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 ||operate.RYD_Star == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId) || (string.IsNullOrWhiteSpace(operate.RYD_Evaluate)&&operate.RYD_Star < 2))
            {
                return NotFound();
            }
            if (_demandOperate.EvaluationOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/ReceivingOrders")]
        public IActionResult ReceivingOrders(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId))
            {
                return NotFound();
            }
            if (_demandOperate.ReceivingOrders(operate))
            {

                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/CompanyTransferOrders")]
        public IActionResult CompanyTransferOrders(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || operate.RYD_ToType == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId) || string.IsNullOrWhiteSpace(operate.RYD_ToCompany))
            {
                return NotFound();
            }
            var typeLinkList = _demandTypeLink.GetDemandTypeLink(operate.RYD_ToCompany, new string[] { operate.RYD_ToType + "" });
            if (typeLinkList.Count == 0)
            {
                return NotFound();
            }
            operate.RYD_ToUserId = _demand.AssignPersonnel(typeLinkList);
            if (string.IsNullOrWhiteSpace(operate.RYD_ToUserId))
            {
                return NotFound();
            }
            if (_demandOperate.CompanyTransferOrders(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/CancelOrder")]
        public IActionResult CancelOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId))
            {
                return NotFound();
            }
            if (_demandOperate.CancelOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/DeleteOrder")]
        public IActionResult DeleteOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId))
            {
                return NotFound();
            }
            if (_demandOperate.DeleteOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/CompletionOrder")]
        public IActionResult CompletionOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId))
            {
                return NotFound();
            }
            if (string.IsNullOrWhiteSpace(operate.RYD_Reason) || string.IsNullOrWhiteSpace(operate.RYD_Solution)||string.IsNullOrWhiteSpace(operate.RYD_Company))
            {
                return NotFound();
            }
            //此处加入知识库
            if (!string.IsNullOrWhiteSpace(operate.RYD_KBId) && operate.RYD_KBId == "true")
            {
                string kbId = _knowledgeBase.AddKnowledgeBase(operate);
                if (string.IsNullOrWhiteSpace(kbId))
                {
                    return NotFound();
                }
                operate.RYD_KBId = kbId;
            }
            else
            {
#pragma warning disable CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
                operate.RYD_KBId = null;
#pragma warning restore CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
            }
            //end


            if (_demandOperate.CompletionOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/FallbackOrder")]
        public IActionResult FallbackOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId)||string.IsNullOrWhiteSpace(operate.RYD_Fallback))
            {
                return NotFound();
            }
            if (_demandOperate.FallbackOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPut("/api/UpdateTypeOrder")]
        public IActionResult UpdateTypeOrder(M_DemandOperate operate)
        {
            if (operate.RYD_DemandId == 0 || string.IsNullOrWhiteSpace(operate.RYD_UserId) || operate.RYD_ToType==0)
            {
                return NotFound();
            }
            if (_demandOperate.UpdateTypeOrder(operate))
            {
                return Ok(true);
            }
            return NotFound();
        }
    }
}
